﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.MySQL;
using Models;
using PetaPoco;

namespace Core
{
    public class navCore
    {
        private readonly navDAL dal = new navDAL();

        public navEntity GetOne(int id)
        {
            return dal.GetOne(id) == null ? new navEntity() : dal.GetOne(id);
        }

        public List<navEntity> GetTree(int type=0)
        {
            return GetTreeNav(0,type);
        }

        public navEntity Save(navEntity model)
        {
            return dal.Save(model);
        }

        public int Update(object obj,int ID)
        {
            return dal.Update(obj, ID);
        }

        public bool Del(int id)
        {
            return dal.Del(id);
        }
        public bool Del(navEntity model)
        {
            return dal.Del(model);
        }

        public List<navEntity> GetTreeNav(int fid,int type=0)
        {
            string where = " and classid=@0";
            if (type == 1)//PC
            {
                where += " and ismobile=0";
            }
            else if (type == 2)
            {
                where += " and ismobile=1";
            }
            var rootList = dal.GetList(where, "ordnum asc,ID asc", fid);
            foreach (navEntity navEntity in rootList)
            {
                navEntity.children = GetTreeNav(navEntity.ID,type);
            }
            return rootList;
        }
        public List<navEntity> GetList(int top, string where, string order, params object[] args)
        {
            if(top>0)
                return dal.GetPageList(1, top, where, order, args).Items;
            return dal.GetList(where, order, args);
        }
        public Page<navEntity> GetPageList(int page,int pagesize, string where, string order, params object[] args)
        {
            return dal.GetPageList(page, pagesize, where, order, args);
        }
    }
}
